﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XinYuan.Office.Models;
namespace XinYuanOfficeWinForm.Admin.Permission
{
    public partial class FrmEditOperator : Form
    {
        
        XinYuanOfficeWinForm.OperatorServiceReference.OperatorServiceClient opeClient = new OperatorServiceReference.OperatorServiceClient();
        public FrmEditOperator()
        {
            InitializeComponent();
        }

        //添加
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!CheckInput())
                return;
            Operator ope = new Operator();
            ope.OperatorName = txtLoginName.Text.Trim();
            ope.Password = txtPassword.Text.Trim();
            ope.RoleId = Convert.ToInt32(lblRoleId.Text);
            if (chkState.Checked)
            {
                ope.State = true;
            }
            else
            {
                ope.State = false;
            }
            if (opeClient.Insert(ope) == 1)
            {
                MessageBox.Show("添加成功");
                ClearControls();
            }

        }
        #region 非空验证
        /// <summary>
        /// 非空验证
        /// </summary>
        /// <returns></returns>
        bool CheckInput()
        {
            if (txtLoginName.Text.Trim().Length == 0 || txtPassword.Text.Trim().Length == 0 || txtRPassword.Text.Trim().Length == 0)
            {
                MessageBox.Show("用户名或密码不能为空", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if (txtPassword.Text.Trim() != txtRPassword.Text.Trim())
            {
                MessageBox.Show("两次输入的密码不一致", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if (txtPassword.Text.Trim().Length<6)
            {
                MessageBox.Show("密码长度不能低于6位", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if (lblRoleId.Text == "")
            {
                MessageBox.Show("请选择角色", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        } 
        #endregion


        #region 清空控件
        void ClearControls()
        {
            txtLoginName.Text = "";
            txtPassword.Text = "";
            txtRPassword.Text = "";
            chkState.Checked = false;
            txtLoginName.Focus();
        }
        
        #endregion
        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmEditOperator_Load(object sender, EventArgs e)
        {
            lblRoleId.Text = "";
        }

        private void btnFindRole_Click(object sender, EventArgs e)
        {
            FrmRoleList frmRoleList = new FrmRoleList();
            frmRoleList.ShowDialog();
            txtRoleName.Text = frmRoleList.roleName;
            lblRoleId.Text =Convert.ToString(frmRoleList.roleId);
        }
    }
}
